import request from '@/utils/request'
import type {
  HasSpuResponseData,
  AllTrademarkResponseData,
  spuImageListResponseData,
  spuSaleAttrListResponseData,
  AllBaseSaleAttrResponseData,
  SpuData,
  SkuInfoResponseData,
} from './type'

const API = {
  HASSPU_URL: '/admin/product/',
  ALLTRADEMARK_URL: '/admin/product/baseTrademark/getTrademarkList',
  IMAGE_URL: '/admin/product/spuImageList/',
  SPUSALEATTR: '/admin/product/spuSaleAttrList/',
  ALLBASESALEATTR: '/admin/product/baseSaleAttrList',
  ADDSPU_URL: '/admin/product/saveSpuInfo',
  UPDATE_URL: '/admin/product/updateSpuInfo',
  ADDSKU_URL: '/admin/product/saveSkuInfo',
  SKUINFO_URL: '/admin/product/findBySpuId/',
  REMOVESPU_URL: '/admin/product/deleteSpu/',
}

//获取某个三级id下的spu
export const reqHasSpu = async (
  page: number,
  limit: number,
  category3Id: string | number,
) => {
  return await request.get<any, HasSpuResponseData>(
    API.HASSPU_URL + `${page}/${limit}?category3Id=${category3Id}`,
  )
}

//获取所有品牌列表
export const reqAllTrademark = async () => {
  return await request.get<any, AllTrademarkResponseData>(API.ALLTRADEMARK_URL)
}

//获取该spu下所有商品的图片
export const reqSpuImageList = async (spuId: number) => {
  return await request.get<any, spuImageListResponseData>(API.IMAGE_URL + spuId)
}

//获取该spu下商品的销售属性
export const reqSpuSaleAttr = async (spuId: number) => {
  return await request.get<any, spuSaleAttrListResponseData>(
    API.SPUSALEATTR + spuId,
  )
}

//获取全部销售属性
export const reqAllBaseSaleAttr = async () => {
  return await request.get<any, AllBaseSaleAttrResponseData>(
    API.ALLBASESALEATTR,
  )
}

export const reqAddOrUpdateSpuInfo = async (data: SpuData) => {
  if (data.id) {
    return await request.post<any, any>(API.UPDATE_URL, data)
  } else {
    return await request.post<any, any>(API.ADDSPU_URL, data)
  }
}

export const reqSaveSkuInfo = async (data: any) => {
  return await request.post<any, any>(API.ADDSKU_URL, data)
}

//获取某个spu下的sku列表
export const reqSkuInfo = async (spuId: number | string) => {
  return await request.get<any, SkuInfoResponseData>(API.SKUINFO_URL + spuId)
}

//根据spuId删除某个spu
export const reqRemoveSpu = async (spuId: number | string) => {
  return await request.delete<any, any>(API.REMOVESPU_URL + spuId)
}
